import Vue from 'vue'
import Vuex from 'vuex'
import {request} from "../api/request";


import createPersistedState from 'vuex-persistedstate'

Vue.use(Vuex);

export default new Vuex.Store({
    state: {
        studentInfo: {
            userId: null,
            stuId: null,
            userName: null,
            trueName: null,
            email: null,
            phone: null,
            address: null,
            gender: null,
            age: null,
            idCard: null,
            school: null
        },
        teacherInfo: {}
    },
    mutations: {
        updateStudentInfo(state, studentData) {
            state.studentInfo.userId = studentData.userId;
            state.studentInfo.stuId = studentData.stuId;
            state.studentInfo.userName = studentData.userName;
            state.studentInfo.trueName = studentData.trueName;
            state.studentInfo.email = studentData.email;
            state.studentInfo.phone = studentData.phone;
            state.studentInfo.address = studentData.address;
            state.studentInfo.gender = studentData.gender;
            state.studentInfo.age = studentData.age;
            state.studentInfo.idCard = studentData.idCard;
            state.studentInfo.school = studentData.school;
        },
        updateTeacherInfo(state, teacherData) {

        }
    },
    actions: {
        updateStudent(context) {
            request({
                method: 'post',
                headers: {
                    'Authorization': window.localStorage.getItem("token")
                },
                url: '/student/check_login'
            }).then(function (data) {
                data = data.data;
                console.log(data);
                if (data.code === 200) {
                    context.commit('updateStudentInfo', data.data);
                }
            });

        }
    },
    modules: {},
    getters: {
        getStudentInfo(state) {
            return state.studentInfo;
        },
        getStudentSchool(state) {
            return state.studentInfo.school;
        },
        getStudentId(state) {
            return state.studentInfo.stuId;
        },
        getStudentTrueName(state) {
            return state.studentInfo.trueName;
        }
    },
    plugins: [createPersistedState()],
})
